package com.bootdo.lvyou.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.lvyou.domain.LvyouCate;
import com.bootdo.lvyou.service.LvyouCateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouCateImg;
import com.bootdo.lvyou.service.LvyouCateImgService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 轮播图表
 * 
 * @author lujingqian
 * @email 965858129@qq.com
 * @date 2019-10-10 13:45:17
 */
 
@Controller
@RequestMapping("/lvyou/lvyouCateImg")
public class LvyouCateImgController extends BaseController{

	public static final String CATELOGO="foodImg";//轮播图
	public static final String CAIPINLOGO="greensImg";//轮播图

	@Autowired
	private LvyouCateImgService lvyouCateImgService;
	@Autowired
	private LvyouCateService lvyouCateService;

	@ModelAttribute
	public LvyouCateImg get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouCateImgService.get(id);
		}else{
			return new LvyouCateImg();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouCateImg','lvyou:lvyouCateImg:lvyouCateImg',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouCateImg:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouCateImg:lvyouCateImg")	
	String LvyouCateImg(LvyouCateImg lvyouCateImg,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouCateImg/lvyouCateImg";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouCateImg:lvyouCateImg")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouCateImg> lvyouCateImgList = lvyouCateImgService.list(query);
		int total = lvyouCateImgService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouCateImgList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouCateImg:edit")
	String add(LvyouCateImg lvyouCateImg,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouCateImg/lvyouCateImgedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouCateImg lvyouCateImg) {
		return R.ok().put("i",lvyouCateImg);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouCateImg:edit")
	public R save( LvyouCateImg lvyouCateImg){


		Long sourceId = lvyouCateImg.getSourceId();
		LvyouCate lvyouCate = lvyouCateService.get(sourceId);

		lvyouCateImg.setSourceName(lvyouCate.getName());
		lvyouCateImg.setSourceLogo(CATELOGO);        //商家轮播图标识

		List<LvyouCateImg> lunbo = lvyouCateImgService.getByIdAll(sourceId, CATELOGO);
		List<LvyouCateImg> caipin = lvyouCateImgService.getByIdAll(sourceId, CAIPINLOGO);
		if(lunbo!=null&&caipin!=null){
			int lunboNumber = lunbo.size();
			int caipinNumber = caipin.size();
			if(lunboNumber>=3&&caipinNumber>=3){
				if(caipinNumber+lunboNumber>9){
					if(lvyouCate.getGive()!=5){
						int number=lvyouCate.getGive()+1;
						lvyouCate.setGive(number);

						lvyouCateService.save(lvyouCate);
					}

				}
			}
		}

		if(lvyouCateImgService.save(lvyouCateImg)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 商家菜品展示
	 */
	@ResponseBody
	@PostMapping("/saveBycaipin")
	@RequiresPermissions("lvyou:lvyouCateImg:edit")
	public R saveBycaipin( LvyouCateImg lvyouCateImg){

		Long sourceId = lvyouCateImg.getSourceId();
		LvyouCate lvyouCate = lvyouCateService.get(sourceId);

		lvyouCateImg.setSourceName(lvyouCate.getName());
		lvyouCateImg.setSourceLogo(CAIPINLOGO);        //商家菜品图片

		List<LvyouCateImg> lunbo = lvyouCateImgService.getByIdAll(sourceId, CATELOGO);
		List<LvyouCateImg> caipin = lvyouCateImgService.getByIdAll(sourceId, CAIPINLOGO);
		if(lunbo!=null&&caipin!=null){
			int lunboNumber = lunbo.size();
			int caipinNumber = caipin.size();
			if(lunboNumber>=3&&caipinNumber>=3){
				if(caipinNumber+lunboNumber>9){
					if(lvyouCate.getGive()!=5){
						int number=lvyouCate.getGive()+1;
						lvyouCate.setGive(number);

						lvyouCateService.save(lvyouCate);
					}

				}
			}
		}
		if(lvyouCateImgService.save(lvyouCateImg)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouCateImg:edit")
	public R remove( Long id){
		if(lvyouCateImgService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouCateImg:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouCateImgService.batchRemove(ids);
		return R.ok();
	}
	
}